package Appointment;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Appointment {
	public static void main(String[] args) throws FileNotFoundException {
		String file = "src/Appointment/C-large.in";
		//String file = "src/Appointment/C-small-attempt2.in";
		//String file = "src/Appointment/test.txt";
		Scanner sc = new Scanner(new File(file));

		int nSets;
		nSets = sc.nextInt();
		sc.nextLine(); // go next line

		int N, P, M;
		Queue<Integer> q = new LinkedList<Integer>();
		for (int i = 1; i <= nSets; i++) {
			N = sc.nextInt();
			P = sc.nextInt();
			M = sc.nextInt();

			int[] friendV = new int[P];			// velocity of friends
			int[] friendCurrPos = new int[P]; 	// current position of friends
			int[][] road = new int[N + 1][N + 1];

			for (int j = 0; j < P; j++) {
				friendCurrPos[j] = sc.nextInt();
				friendV[j] = sc.nextInt();
			}

			for (int j = 0; j < M; j++) {
				int len = sc.nextShort();
				int prev = 0;
				int curr = 0;
				for (int k = sc.nextInt(); k > 0; k--) {
					curr = sc.nextInt();

					if (prev != 0) {
						road[prev][curr] = len;
						road[curr][prev] = len;
						
					} 
					prev = curr;
				}
			}

			// init travel.
			int[][] travel = new int[P][N + 1];
			for (int j = 0; j < P; j++) {
				for (int k = 0; k < N + 1; k++) {
					travel[j][k] = -1;
				}
			}

			// start traverse!
			for (int j = 0; j < P; j++) {
				travel[j][friendCurrPos[j]] = 0;

				q.add(friendCurrPos[j]);
				while (!q.isEmpty()) {
					friendCurrPos[j] = q.poll();
					for (int k = 1; k <= N; k++) {
						if (road[friendCurrPos[j]][k] >= 1) {
							int time = travel[j][friendCurrPos[j]]
									+ (road[friendCurrPos[j]][k] * friendV[j]);

							if (travel[j][k] == -1 || time < travel[j][k] ) {
								travel[j][k] = time;
								q.add(k);
							}
						}
					}
				}
			}

			int slowFriend = 0;
			int min = Integer.MAX_VALUE;
			// find minimum time
			for (int k = 1; k <= N; k++) {
				slowFriend = 0;
				for (int j = 0; j < P; j++) {
					if (travel[j][k] == -1) {
						slowFriend = Integer.MAX_VALUE;
						break;
					}

					if (travel[j][k] > slowFriend ) {
						slowFriend = travel[j][k]; 
					}
				}
				if (slowFriend < min) {
					min = slowFriend;
				}
			}

			if (min == Integer.MAX_VALUE) {
				min = -1;
			}

			System.out.println("Case #" + i + ": " + min);
		}
		sc.close();
	}
}
